module.exports = options => {
    const jwt = require('jsonwebtoken')
    const Admin = require('../models/admin')
    return async(req, res, next) => {
        // 获取token 
        const token = String(req.headers.authorization || '').split(' ').pop()
        if (!token) return res.status(401).send({
                message: '请先登录'
        })
        // token校验
        const {id,expiresIn,ctime} = jwt.verify(token, req.app.get('token'))
        //获取当前时间
        const nowTime = parseInt(new Date().getTime() / 1000)
        //过期时间
        const exp = ctime + expiresIn
        let cha = exp - nowTime
        if (cha < 0) return res.status(401).send({
            message:'token过期'
        })

        const tokenAdmin = await Admin.findById(id)
        if (!tokenAdmin) return res.status(401).send({
            message: '无效token'
        })
        await next()
    }



}